package lanou.giftsay_01.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.junit.experimental.categories.Category;

import lanou.giftsay_01.R;
import lanou.giftsay_01.bean.CategoryBean;
import lanou.giftsay_01.fragment.categorybody.OnClickCateGoryItem;

/**
 * Created by dllo on 16/8/18.
 */
public class GonglueRvAdapter extends RecyclerView.Adapter<GonglueRvAdapter.MyViewHolder> {
    private CategoryBean categoryBean ;
    private Context context ;
    private OnClickCateGoryItem  onClickCateGoryItem;

    public void setOnClickCateGoryItem(OnClickCateGoryItem onClickCateGoryItem) {
        this.onClickCateGoryItem = onClickCateGoryItem;
    }

    public void setCategoryBean(CategoryBean categoryBean) {
        this.categoryBean = categoryBean;
        notifyDataSetChanged();
    }

    public GonglueRvAdapter(Context context ) {
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_gonglue, parent ,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
            holder.rl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickCateGoryItem.onClick(position ,categoryBean);
                }
            });
        if (position<11) {
            try {
                holder.title.setText(categoryBean.getData().getColumns().get(position).getTitle());
                holder.name.setText(categoryBean.getData().getColumns().get(position).getAuthor());
                Picasso.with(context).load(categoryBean.getData().getColumns().get(position).getCover_image_url()).into(holder.iv);
                holder.subtitle.setText(categoryBean.getData().getColumns().get(position).getSubtitle());
            } catch (NullPointerException e) {

            }
        }else {
            holder.iv.setImageResource(R.mipmap.sreachall);
            holder.title.setText("");
            holder.name.setText("");
            holder.subtitle.setText("");
        }

    }

    @Override

    public int getItemCount() {
        return 12;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl;
        TextView title ;
        ImageView iv ;
        TextView name ;
        TextView subtitle;

        public MyViewHolder(View itemView) {
            super(itemView);

            rl = (RelativeLayout) itemView.findViewById(R.id.rl_item);
            iv = (ImageView) itemView.findViewById(R.id.iv_gonglue);
            title = (TextView) itemView.findViewById(R.id.tv_title);
            name = (TextView) itemView.findViewById(R.id.tv_name_gonglue);
            subtitle = (TextView) itemView.findViewById(R.id.subtitle_tv);
        }
    }

}
